from scapy.all import IP, Ether, sniff, Packet, bind_layers, Raw
from scapy.fields import ByteField, ShortField, StrField, IntField


def packet_handler(packet):
    if IP in packet:
        print(f"Received IP Packet: {packet[IP].summary()}")
        print(f"Source IP: {packet[IP].src}")
        print(f"Destination IP: {packet[IP].dst}")
        print(f"Payload: {packet[IP].payload}")
        if packet.haslayer(Ether):  # 检查是否有以太网层
            print(f"Source MAC: {packet[Ether].src}")  # 获取源 MAC 地址
            print(f"Destination MAC: {packet[Ether].dst}")  # 获取目标 MAC 地址
        # print the whole packet
        print(packet.show())


if __name__ == "__main__":
    # 开始嗅探 IP 数据包，指定监听的网络接口
    sniff(iface="enp5s0f0np0", filter="ip", prn=packet_handler, store=0)